﻿using System.Collections.Specialized;
using System.Collections;
using System.IO;
using System;

namespace  HF.Service
{

    public class TemplateHelper 
    {
        /// <summary>        
        /// 私有构造方法，不允许创建实例       
        /// </summary>        
         private TemplateHelper()        
         {            
             // TODO: Add constructor logic here        
         }
         /// <summary>        
         /// Template File Helper         
         /// </summary>        
         /// <param name="templatePath">Templet Path</param>        
         /// <param name="values">NameValueCollection</param>        
         /// <returns>string</returns>        
         public static string BulidByFile(string templatePath, NameValueCollection values)
         {
             return BulidByFile(templatePath, values, "[$", "]");
         }
         /// <summary>        
         ///         
         /// </summary>        
         /// <param name="template"></param>        
         /// <param name="values">NameValueCollection obj</param>        
         /// <param name="prefix"></param>        
         /// <param name="postfix"></param>        
         /// <returns></returns>        
         public static string Build(string template, NameValueCollection values, string prefix, string postfix)
         {
             if (values != null)
             {
                 foreach (DictionaryEntry entry in values)
                 {
                     template = template.Replace(string.Format("{0}{1}{2}", prefix, entry.Key, postfix), entry.Value.ToString());
                 }
             }
             return template;
         }
         /// <summary>        
         ///         
         /// </summary>        
         /// <param name="templatePath"></param>        
         /// <param name="values"></param>        
         /// <param name="prefix"></param>        
         /// <param name="postfix"></param>        
         /// <returns></returns>        
         public static string BulidByFile(string templatePath, NameValueCollection values, string prefix, string postfix)
         {
             StreamReader reader = null;
             string template = string.Empty;
             try
             {
                 reader = new StreamReader(templatePath);//(@"E:\HF2.0\source\Server\HF.Service.Test\bin\Debug\mailtemplate\V_TrActivityCourse.txt");
                 template = reader.ReadToEnd();
                 reader.Close();
                 if (values != null)
                 {
                     foreach (string key in values.AllKeys)
                     {
                         template = template.Replace(string.Format("{0}{1}{2}", prefix, key, postfix), values[key]);
                     }
                 }
             }
             catch (Exception ex) { throw ex; }
             finally
             {
                 if (reader != null)
                     reader.Close();
             }
             return template;
         }

         //private void SendMail(string email, string name, string sdate, string edate, string course, string point)
         //{
         //    string mailFrom = MailHelper.MailServerFrom;
         //    string mailTo = string.Empty;
         //    string mailSubject = string.Empty;
         //    string mailBody = string.Empty;
         //    string mailAttch = string.Empty;
         //    string mailCode = string.Empty;
         //    string mailPriority = string.Empty;
         //    string mailCC = string.Empty;
         //    string resultMessage = "";
         //    if (!string.IsNullOrEmpty(email))
         //    {
         //        mailTo = email;
         //        mailSubject = "Change By Innovation 加分通知";
         //        try
         //        {
         //            string templetpath = Server.MapPath("../mailtemplate/irupoint.txt");
         //            NameValueCollection myCol = new NameValueCollection();
         //            myCol.Add("ename", name);
         //            myCol.Add("sdate", sdate);
         //            myCol.Add("edate", edate);
         //            myCol.Add("course", course);
         //            myCol.Add("point", point);
         //            myCol.Add("link", System.Configuration.ConfigurationManager.AppSettings["rootUrl"]);
         //            mailBody = TemplateHelper.BulidByFile(templetpath, myCol);
         //            MailHelper.SendNetMail(mailFrom, mailTo, mailSubject, mailBody, mailAttch, mailCode, mailPriority, mailCC, out resultMessage);
         //        }
         //        catch (Exception ex)
         //        {
         //        }
         //    }
         //}
    }
}
